package com.xxx.car.web.admin;

import com.xxx.car.entity.vo.request.ReqAdminLogin;
import com.xxx.car.service.UserService;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * Created by yxj on 17/6/26.
 */
@Controller
@RequestMapping("/login")
public class AdminLoginController {

	@Autowired
	UserService userService;

	@RequestMapping(value = "/")
	public String login(Model model, HttpSession httpSession,@ModelAttribute ReqAdminLogin reqAdminLogin, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			model.addAttribute("msg", ApiResponseEnum.ACCOUNT_OR_PASSWORD_ERROR);
		}
		APIResponse response = userService.adminUserLogin(reqAdminLogin);
		if (response.isRet()){
			httpSession.setAttribute("cur_user",response.getData());
		} else {
			model.addAttribute("msg",response.getErrmsg());
			return "login";
		}
		return "redirect:/admin/";
	}

	@RequestMapping(value = "/logout")
	public String logout(Model model, HttpSession httpSession) {
		httpSession.removeAttribute("cur_user");
		return "login";
	}
}
